package com.bsj.media.media.packageFlv;



import com.bsj.media.media.ParserVideoData;
import com.bsj.media.tool.cmd.CmdReqString;
import com.bsj.power.common.def.protocol.rtp.RtpParameterSet;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.PropertiesUtil;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @Description 音视频flv处理
 * @Date 2022/10/25 14:48
 **/
@Slf4j
public class MediaFlvHandler extends FlvTypeHandler{

    /**
     * 用于开启：开始打包flv前需接收到音频
     */
//    private static boolean isStartUpAudioJudge = PropertiesUtil.getConfigAsBoolean("isStartUpAudioJudge");
    private boolean hasGetAudioType; // 获取音频类型
    private AtomicInteger iFrameTime = new AtomicInteger(); // 连续1次I帧后，没有音频数据，则使用默认类型
    private CacheParams cacheParamsNow;

    public MediaFlvHandler(CacheParams cacheParams) {
        super((byte) 0x05);
        cacheParamsNow = cacheParams;
    }

    @Override
    public boolean preJudgement(byte[] fdBody) {
        if (cacheParamsNow.getProtocolType() == CmdReqString.PROTOCOL_TYPE_FD) {
            if (!hasGetAudioType) {
                if (ParserVideoData.getTypeFrame(fdBody) == 0) {
                    // 有音频
                    audioType = ParserVideoData.getAudioType(fdBody);
                    if (audioType == 3) {
                        st_audioRate = 64;
                    }
                    hasGetAudioType = true;
                } else if (ParserVideoData.checkIframe(fdBody)) {
                    iFrameTime.incrementAndGet();
                }

                if (!hasGetAudioType) {
                    // 判断i帧接收情况
                    if (iFrameTime.get() < 2 && ParserVideoData.checkIframe(fdBody)) {
                        log.warn("收到1次I帧，还没有音频包 ");
                    } else {
                        mediaToVideoPacket = true;
                    }
                    return false;
                }
            }
        }

        if (!ParserVideoData.checkIframe(fdBody)) {
            // 如果是只有音频的时候不判断这个
            log.warn("打包头---不是I帧，丢掉, 帧类型："+ ParserVideoData.getTypeFrame2(fdBody) +"---key:" );
            return false;
        }
        int videoRate = ParserVideoData.getVideoFrame(fdBody, 0);
        if (videoRate > 0) {
            st_videoRate = 1000 / videoRate;
        }
        getSolutionRatio(ParserVideoData.getVideoSolution(fdBody, 0));

        // 获取帧率
        videoFrameRate = ParserVideoData.getVideoFrame(fdBody, 0) != 0 ? ParserVideoData.getVideoFrame(fdBody, 0) : 15;
        videocodecid = ParserVideoData.getAudioType(fdBody) == RtpParameterSet.fdVideoCodeTypeH265 ? RtpParameterSet.flvCodecIdH265 : RtpParameterSet.flvCodecIdH264;

        // 音频采样大小
        audioSampleRate = audioType == 3 ? 16000 : 41000;
        log.info("开始打包: uri" + ", 帧率:" + videoFrameRate + ", 视频类型:" + ((videocodecid == RtpParameterSet.flvCodecIdH265) ? "h265" : "h264") + ", 采样率:" + audioSampleRate);
        return true;
    }

    @Override
    public void setFristMediaTag(byte[] fdBody, ByteBuffer dst) {
        // 音频和视频都需要打包 , 这里fdBody是视频I帧的数据
        // 1、视频Tag[0]打包
        if (fdBody[2] == RtpParameterSet.fdVideoCodeTypeH265) {
            h265FristTagFlv(fdBody, dst);
        } else {
            h264FristTagFlv(fdBody, dst);
        }
        // 2、音频Tag[0]打包
        if (dst.position() != 0) {
            // 表示视频打包成功
            aacAudioFristTagFlv(dst);
        }
    }

    @Override
    public boolean packVideoFlag() {
        return true;
    }

    @Override
    public boolean packAudioFlag() {
        return true;
    }


}
